#pragma once
#include<iostream>
#include<assert.h>
#include"reverseiterator.h"
using namespace std;

namespace yjt
{
	template<class T>
	struct ListNode
	{
		ListNode<T>* _prev;
		ListNode<T>* _next;
		T _data;

		ListNode(const T& x=T())
			:_prev(nullptr)
			,_next(nullptr)
			,_data(x)
		{}
	};
	template<class T,class Ref,class Ptr>
	struct __list_iterator
	{
		typedef ListNode<T> Node;
		typedef __list_iterator<T,Ref,Ptr> self;
		Node* _node;
		__list_iterator(Node* node)
			:_node(node)
		{}
		self& operator++()
		{
			_node = _node->_next;
			return *this;
		}
		self operator++(int)
		{
			self tmp(*this);
			_node = _node->_next;
			return tmp;
		}
		self& operator--()
		{
			_node = _node->_prev;
			return *this;
		}
		self operator--(int)
		{
			self tmp(*this);
			_node = _node->_prev;
			return tmp;
		}
		Ref operator*() { return _node->_data; }
		Ptr operator->() { return &_node->_data; }
		bool operator!=(const self& s) { return _node != s._node; }
	};
	template<class T>
	class list
	{
		typedef ListNode<T> Node;
	public:
		typedef __list_iterator<T, T&, T*> iterator;
		typedef __list_iterator<T, const T&, const T*> const_iterator;
		typedef ReverseIterator< iterator, T&, T*> reverse_iterator;
		typedef ReverseIterator< iterator, const T&, const T*> const_reverse_iterator;

		reverse_iterator rbegin(){ return reverse_iterator(end()); }
		reverse_iterator rend(){ return reverse_iterator(begin()); }
		const_reverse_iterator rbegin() const { return reverse_iterator(end()); }
		const_reverse_iterator rend() const { return reverse_iterator(begin()); }
		iterator begin() { return _head->_next; }
		iterator end() { return _head; }
		const_iterator begin() const { return _head->_next; }
		const_iterator end() const { return _head; }
		void empty_init()
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
		}
		list()
		{
			empty_init();
		}
		//list(const list<T>& lt)
		list(list<T>& lt)
		{
			empty_init();
			for (const auto& e : lt)
			{
				push_back(e);
			}
		}
		void swap(list<T>& tmp)
		{
			std::swap(_head,tmp._head);
		}
		//list<T>& operator=(const list<T>& lt)
		list<T>& operator=(list<T> lt)
		{
			swap(lt);
			return *this;
		}
		void clear()
		{
			iterator it = begin();
			while (it != end())
			{
				it = erase(it);
				++it;
			}
		}
		~list()
		{
			clear();
			delete _head;
			_head = nullptr;
		}
		//void push_back(const T& x)
		//{
		//	Node* newnode = new Node(x);
		//	Node* tail = _head->_prev;

		//	tail->_next = newnode;
		//	newnode->_prev = tail;
		//	newnode->_next = _head;
		//	_head->_prev = newnode;
		//}
		void push_back(const T& x){ insert(end(), x); }
		void push_fornt(const T& x){ insert(begin(), x); }
		void pop_back(){ erase(--end()); }
		void pop_front(){ erase(begin()); }
		iterator insert(iterator pos, const T& x)
		{
			Node* newnode = new Node(x);
			Node* prev = pos._node->_prev;

			newnode->_prev = prev;
			prev->_next = newnode;
			newnode->_next = pos._node;
			pos._node->_prev = newnode;

			return newnode;
		}

		iterator erase(iterator pos)
		{
			assert(pos != end());
			Node* cur = pos._node;
			Node* prev = cur->_prev;
			Node* next = cur->_next;
			prev->_next = next;
			next->_prev = prev;
			delete cur;

			return next;
		}

	private:
		Node* _head;
	};
	void list_print(const list<int>& lt)
	{
		list<int>::const_iterator it = lt.begin();
		while (it != lt.end())
		{
			cout << *it << " ";
			++it;
		}
		cout << endl;
	}

	void test_list1()
	{
		list<int> l1;
		l1.push_back(1);
		l1.push_back(2);
		l1.push_back(3);
		l1.push_back(4);

		list<int>::iterator it = l1.begin();
		while (it != l1.end())
		{
			cout << *it << " ";
			++it;
		}
		cout << endl;

		l1.insert(l1.begin(), 0);
		l1.insert(l1.begin()++, 30);
		l1.insert(l1.end(), 50);

		list<int>::iterator it1 = l1.begin();
		while (it1 != l1.end())
		{
			cout << *it1 << " ";
			++it1;
		}
		cout << endl;
	}

	void test_list2()
	{
		list<int> l1;
		l1.push_back(1);
		l1.push_back(2);
		l1.push_back(3);
		l1.push_back(4);

		list<int>::iterator it = l1.begin();
		while (it != l1.end())
		{
			cout << *it << " ";
			++it;
		}
		cout << endl;

		l1.push_back(200);
		l1.push_fornt(400);
		for (auto x : l1)
		{
			cout << x << " ";
		}
		cout << endl;

		l1.pop_back();
		l1.pop_front();
		for (auto x : l1)
		{
			cout << x << " ";
		}
		cout << endl;
		list<int> l2(l1);
		list<int> l3;
		l3 = l2;
	}

}